import { queryMenuData, queryMenuRoleList, queryAppList } from '@/services/user';
import { tranfarTree } from '@/utils/common';
import store from 'store';
import menuListData from '../../config/menu';

const UserModel = {
  namespace: 'menu',
  state: {
    menuListData: [],
    menuTreeData: [],
    appsList: []
  },
  effects: {
    *queryMenuData(_, { call, put }) {
      const { data } = yield call(queryMenuData, { appCode: 'OPERATION' });
      const { data: menuBtnAuthData } = yield call(queryMenuRoleList, { appCode: 'OPERATION' });
      const { data: appsList } = yield call(queryAppList, { appCode: 'OPERATION' })
      const newData = data
        .map(el => ({
          ...el,
          key: el.path,
          icon: 'icon-systemmanager',
          hideInMenu: false
        }));

      const menuTreeData = tranfarTree(newData, 'id', 'parentId');

      // 菜单数据
      yield put({
        type: 'updateState',
        payload: {
          menuListData: newData,
          menuTreeData,
          appsList
        }
      });

      // 按钮权限数据
      store.set('auth', menuBtnAuthData ? menuBtnAuthData.permissions : []);
      return menuTreeData
    },
  },
  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
  }
};
export default UserModel;
